package dao;

import java.util.LinkedList;
import java.util.List;

import relations.Associate;
import relations.belongsTo;
import entities.Media_Company;
import entities.Product;
import entities.album;
import entities.order;
import entities.User;

public class AssociateDAO {

	private static final AssociateDAO instance = new AssociateDAO();
	private List<Product> AssociateProduct;
	private List<Media_Company>AssociateMediaComp;
	
	private AssociateDAO(){
		AssociateProduct = new LinkedList<Product>();
		AssociateMediaComp = new LinkedList<Media_Company>();
		Associate cus = null;
		
		//cus = Associate.newInstance("jaime", "Jaime", "Vazquez", "ja@j.com", 1L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		//belongsTos.add(cus);
		//cus = belongsTo.newInstance("jaime1", "Jaime", "Vazquez", "ja@j.com", 2L, "test", "kl", "1", "00778", "PR", "Gurabo", "USA");
		//belongsTos.add(cus);
	}
	
	public static AssociateDAO getInstance() {
		return instance;
	}

	public synchronized Associate findAssociate(Long product_id, String name) {
		Product result1 = null;
		Media_Company result2 = null;
		Associate result = null;
		boolean flag1 = false;
		boolean flag2 = false;
		
		for (Product s : AssociateProduct){
			if (s.getProduct_id().equals(product_id)){
				result1 = s;
				flag1 = true;
			}
		}
		for (Media_Company r : AssociateMediaComp){
			if (r.getName().equals(name)){
				result2 = r;
				flag2 = true;
			}
		}
		if (flag1 == true && flag2 == true)
			result = new Associate(result1.getProduct_id(), result2.getName());
	
		return result;
	}

	public synchronized void addAssociate(Product objProduct, Media_Company objMedia) throws RecordDuplicateException{
		if (findAssociate(objProduct.getProduct_id(), objMedia.getName() ) != null){
			throw new RecordDuplicateException("Relation Associate with Product ID: " + objProduct.getProduct_id() + " and" + " Media Company Name: " + objMedia.getName() + " is already in the db.");
		}
		else {
			AssociateProduct.add(objProduct);
			AssociateMediaComp.add(objMedia);
		}
	}

}
